package com.sugon.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.sugon.dao.AnnexFileDao;
import com.sugon.dao.UserSpcFileDao;
import com.sugon.entity.AnnexFileEntity;
import com.sugon.entity.UserSpcFileEntity;
import com.sugon.service.AnnexFileService;
import com.sugon.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 附件表Service实现类
 *
 * @author sugon
 * @date 2019-07-11 10:37:10
 */
@Service("annexFileService")
public class AnnexFileServiceImpl implements AnnexFileService {

    @Autowired
    private AnnexFileDao annexFileDao;
    @Autowired
    private UserSpcFileDao userSpcFileDao;

    /**
     * @param id 主键
     * @return
     */
    @Override
    public AnnexFileEntity queryObject(String id) {
        return annexFileDao.queryObject(id);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<AnnexFileEntity> queryList(Map<String, Object> map) {
        return annexFileDao.queryList(map);
    }

    /**
     * @param map 参数
     * @return
     */
    @Override
    public List<AnnexFileEntity> queryEquList(Map<String, Object> map) {
        return annexFileDao.queryDevEquList(map);
    }

    /**
     * queryEquTotal
     *
     * @param map
     * @return
     */
    @Override
    public int queryEquTotal(Map<String, Object> map) {
        return annexFileDao.queryDevEquTotal(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return annexFileDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param annexFile 实体
     * @return
     */
    @Override
    public int save(AnnexFileEntity annexFile) {
        annexFile.setId(RandomUtil.randomString(32));
        annexFile.setUploadDate(DateUtil.date());
        annexFile.setDelFlag(Constant.IS_NOT_DEL_INT);
        return annexFileDao.save(annexFile);
    }

    /**
     * saveUser
     *
     * @param annexFile
     * @param userId
     * @return
     */
    @Override
    public int saveUser(AnnexFileEntity annexFile, String userId) {
        String fileID = RandomUtil.randomString(32);//附件ID
        UserSpcFileEntity userSpcFile = new UserSpcFileEntity();
        userSpcFile.setFileId(fileID);
        userSpcFile.setUserId(userId);
        userSpcFile.setFileName(annexFile.getFileName());
        userSpcFile.setId(RandomUtil.randomString(32));
        userSpcFileDao.save(userSpcFile);
        annexFile.setUploadDate(new Date());
        annexFile.setId(fileID);
        return annexFileDao.save(annexFile);
    }

    /**
     * update
     *
     * @param annexFile 实体
     * @return
     */
    @Override
    public int update(AnnexFileEntity annexFile) {
        annexFile.setUploadDate(DateUtil.date());
        return annexFileDao.update(annexFile);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return annexFileDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return annexFileDao.deleteBatch(ids);
    }
}
